clear

import delimited "data.csv"

rename v1 id
rename v2 pp
rename v3 site
rename v4 choice 
rename v5 tc
rename v6 phi
rename v7 year
order year, before(pp)
merge m:1 site using "site_char.dta"
drop _merge

gen choice_idx = 0
bysort id: replace choice_idx = 1 if choice==site 
order choice_idx, after(choice)
sort year id site

replace phi = 1 if site == 0
gen lnphi = ln(phi)
replace lnphi = -9999 if lnphi == .

gen sp = "_"
egen uid = concat(id sp year)
drop sp
order uid, after(year)

forvalues idx = 0(1)22 {
	quietly gen chi`idx' = 0
	quietly replace chi`idx' = 1 if site == `idx'
}

gen optout = 0
replace optout = 1 if site == 23 

clogit choice_idx chi* tc lnphi, group(uid)

predict Phat
bysort uid: egen Psum = total(Phat) if site>15 & site<19
replace Psum = . if site!=16
gen WTP = -ln(1-Psum)/_b[tc]*200
sum WTP if choice==16 | choice==17 | choice==18

gen P_jstar = Phat*choice_idx
replace P_jstar = . if P_jstar == 0
forvalues idx = 0(1)8 {
	sum P_jstar if pp == `idx'
}
